package com.xt.service.impl;

import com.xt.bean.Module;
import com.xt.bean.Role;
import com.xt.bean.User;
import com.xt.dao.ModuleRepository;
import com.xt.dao.RoleRepository;
import com.xt.dao.UserRepository;
import com.xt.dto.PermissionDto;
import com.xt.dto.UserListSearchDto;
import com.xt.service.UserService;
import com.xt.utils.Encrypt;
import com.xt.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;

/**
 * Created by XT on 2018/5/3.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository ur;

    @Autowired
    private RoleRepository rr;

    @Autowired
    private ModuleRepository mr;

    @Override
    public void deleteUser(String userid) {
        //先查询数据库中的user
        User oUser = ur.findOne(userid);
        //修改状态为被删除
        oUser.setStatus(-1);
        oUser.setUpdateDate(new Date());

        //更新
        ur.save(oUser);
    }

    @Override
    public void InsertSuperAdmin(String pwd) {
        User user = new User();
        user.setId("SuperAdmin");
        user.setSchoolNumber("SuperAdmin");
        user.setName("SuperAdmin");
        user.setRoleType(3);
        user.setStatus(1);
        user.setPwd(Encrypt.md5(pwd, user.getSchoolNumber()));
        user.setUpdateDate(new Date());
        ur.save(user);
    }

    @Override
    public void saveOrUpdateUser(User user) {
        if (StringUtils.isEmpty(user.getId())) {
            //新增操作，设置密码加密和创建时间
            user.setStatus(1);
            user.setPwd(Encrypt.md5("123456", user.getSchoolNumber()));
            user.setCreateDate(new Date());
        }
        user.setUpdateDate(new Date());
        ur.save(user);
    }

    @Override
    public User getUserById(String id) {
        return ur.findOne(id);
    }

    @Override
    public Page<User> getAllUserListByPage(int count, int pageNo, String sortDirection, String... sortField) {
        Page<User> usersPage = ur.findAll(PageUtil.getPageable(count, pageNo, sortDirection, sortField));
        return usersPage;
    }

    @Override
    public Page<User> getStudentPage(UserListSearchDto ud) {
        Page<User> StudentPage = ur.findByRoleTypeAndDepartmentIdLikeAndSpecialtyIdLikeAndClassIdLikeAndEntranceYearLikeAndNameLikeAndSchoolNumberLike(
                0,
                ud.getDepartmentId(),
                ud.getSpecialtyId(),
                ud.getClassId(),
                ud.getEntranceYear(),
                ud.getName(),
                ud.getSchoolNumber(),
                PageUtil.getPageable(ud.getCount(), ud.getPageNo(), ud.getSortDirection(), ud.getSortField())
        );
        return StudentPage;
    }

    @Override
    public Page<User> getTeacherPage(UserListSearchDto ud) {
//        Page<User> StudentPage = ur.findByNameLikeAndDepartmentIdLike("","",PageUtil.getPageable(ud.getCount(), ud.getPageNo(), ud.getSortDirection(), ud.getSortField()));
        Page<User> StudentPage = ur.findByRoleTypeAndDepartmentIdLikeAndSpecialtyIdLikeAndClassIdLikeAndEntranceYearLikeAndNameLikeAndSchoolNumberLike(
                1,
                ud.getDepartmentId(),
                ud.getSpecialtyId(),
                ud.getClassId(),
                ud.getEntranceYear(),
                ud.getName(),
                ud.getSchoolNumber(),
                PageUtil.getPageable(ud.getCount(), ud.getPageNo(), ud.getSortDirection(), ud.getSortField())
        );
        return StudentPage;
    }

    @Override
    public Page<User> getAdminPage(UserListSearchDto ud) {
        ArrayList<Integer> adminRoles = new ArrayList<>();
        adminRoles.add(2);
        adminRoles.add(3);
        Page<User> StudentPage = ur.findByRoleTypeInAndDepartmentIdLikeAndSpecialtyIdLikeAndClassIdLikeAndEntranceYearLikeAndNameLikeAndSchoolNumberLike(
                adminRoles,
                ud.getDepartmentId(),
                ud.getSpecialtyId(),
                ud.getClassId(),
                ud.getEntranceYear(),
                ud.getName(),
                ud.getSchoolNumber(),
                PageUtil.getPageable(ud.getCount(), ud.getPageNo(), ud.getSortDirection(), ud.getSortField())
        );
        return StudentPage;
    }

    @Override
    public User getUserBySchoolNumber(String schoolNumber) {
        return ur.findBySchoolNumber(schoolNumber);
    }

    @Override
    public User login(User user) {
        User oUser = null;
        if (user.getRoleType() == 0) {
            //学生登陆界面
            oUser = ur.findBySchoolNumberAndPwdAndStatusAndRoleType(user.getSchoolNumber(), Encrypt.md5(user.getPwd(), user.getSchoolNumber()), 1, 0);
        } else if (user.getRoleType() == 1) {
            //教师登陆界面
            oUser = ur.findBySchoolNumberAndPwdAndStatusAndRoleType(user.getSchoolNumber(), Encrypt.md5(user.getPwd(), user.getSchoolNumber()), 1, 1);
        } else if (user.getRoleType() == 2) {
            //管理员登陆界面
            ArrayList<Integer> adminRoles = new ArrayList<>();
            adminRoles.add(2);
            adminRoles.add(3);
            oUser = ur.findBySchoolNumberAndPwdAndStatusAndRoleTypeIn(user.getSchoolNumber(), Encrypt.md5(user.getPwd(), user.getSchoolNumber()), 1, adminRoles);
        }

        return oUser;
    }

    @Override
    public PermissionDto getPermissionByUserId(String userid) {
        PermissionDto permissionDto = new PermissionDto();
        User user = ur.findOne(userid);
        ArrayList<Role> roles = new ArrayList<>();
        ArrayList<Module> modules = new ArrayList<>();
        String[] rolesId = user.getRolesId();
        for (String roleId : rolesId) {
            Role role = rr.findOne(roleId);
            roles.add(role);

            String[] moduleIds = role.getModuleIds();
            for (String module : moduleIds) {
                Module m = mr.findOne(module);
                modules.add(m);
            }
        }
        permissionDto.setUser(user);
        permissionDto.setRoles(roles);
        permissionDto.setModules(modules);
        return permissionDto;
    }

}
